//
//  JMWriteCodeController.m
//  OpenSesame
//
//  Created by zhangjiaming on 15/12/30.
//  Copyright © 2015年 OpenSesame. All rights reserved.
//

#import "JMWriteCodeController.h"
#import "JMWriteCodeCell.h"
#import "JMFrequencyViewController.h"
#import "JMUpView.h"
@interface JMWriteCodeController () <UITableViewDelegate, UITableViewDataSource, JMUpViewDelegate, JMWriteCodeCellDelegate>
@property (weak, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSArray *pictureNameArray;
@property (strong, nonatomic) NSArray *textArray;
@property (weak, nonatomic) JMUpView *upView;
@property (weak, nonatomic) UIButton *confirmRebornBtn;
@property (weak, nonatomic) JMWriteCodeCell *cell;
@end

@implementation JMWriteCodeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航栏
    self.title = @"填写码值";
    
    // 最顶部的灰色间距
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *topBack = [[UIView alloc] initWithFrame:CGRectMake(0, 64, JMPhoneWidth, 10)];
    topBack.backgroundColor = JMColor(242, 242, 242);
    [self.view addSubview:topBack];
    
    // 设置tableView
    UITableView *tableview = [[UITableView alloc] init];
    self.tableView = tableview;
    tableview.frame = CGRectMake(0, 74, JMPhoneWidth, JMPhoneHeight - 40 - 40 - 74); // 40为确认再生按钮的高度, 40为按钮上下空隙，10 为tableview上方空隙
    [self.view addSubview:tableview];
    [tableview setSeparatorColor:JMColor(242, 242, 242)];
    tableview.showsVerticalScrollIndicator = NO;
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableview.dataSource = self;
    tableview.delegate = self;
    
    NSString *image1 = @"dizhima";
    NSString *image2 = @"maikuanma";
    NSString *image3 = @"pinlv";
    NSString *image4 = @"kaisuo";
    NSString *image5 = @"guanmen";
    NSString *image6 = @"zanting";
    NSString *image7 = @"jiesuo";
    NSArray *pictureNameArray = [NSArray arrayWithObjects:image1, image2, image3, image4, image5, image6, image7, nil];
    self.pictureNameArray = pictureNameArray;
    
    NSString *text1 = @"地址码";
    NSString *text2 = @"脉宽码";
    NSString *text3 = @"频率";
    NSString *text4 = @"开键值";
    NSString *text5 = @"关键值";
    NSString *text6 = @"暂停值";
    NSString *text7 = @"解锁值";
    NSArray *textArray = [NSArray arrayWithObjects:text1, text2, text3, text4, text5, text6, text7, nil];
    self.textArray = textArray;
    
    // 设置确定再生按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    float buttonRatio = 71/10;
    CGFloat buttonW = JMPhoneWidth - 20;
    CGFloat buttonH = buttonW/buttonRatio;
    [button setImage:[UIImage imageNamed:@"confirmzaisheng0"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"confirmzaisheng1"] forState:UIControlStateHighlighted];
    button.frame = CGRectMake(10, JMPhoneHeight - buttonH - 20, buttonW, buttonH);
    self.confirmRebornBtn = button;
    
    // 设置上移View
    JMUpView *upView = [[JMUpView alloc] initWithFrame:CGRectMake(0, JMPhoneHeight, JMPhoneWidth, 207)];
    [self.view addSubview:upView];
    upView.delegate = self;
    self.upView = upView;
}

/**
 *  点击确认按钮跳转JMFrequencyViewController
 */
- (void)confirm {
    JMFrequencyViewController *freVc = [[JMFrequencyViewController alloc] init];
    [self.navigationController pushViewController:freVc animated:YES];
    freVc.title = @"再生";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableView数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JMWriteCodeCell *cell = [JMWriteCodeCell cellWithTableView:tableView];
    cell.delegate = self;
    NSString *imageName = self.pictureNameArray[indexPath.row];
    NSString *text = self.textArray[indexPath.row];
    cell.iconVIew.image = [UIImage imageNamed:imageName];
    cell.label.text = text;
    
    if (indexPath.row > 2) {
        cell.inputField.placeholder = @"点击此处修改";
        cell.inputField.userInteractionEnabled = NO;
    } else {
        cell.inputField.placeholder = @"请输入";
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:indexPath];
    return cell.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 回退键盘
    [self.view endEditing:YES];
    // 获取点击处的cell
    JMWriteCodeCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    self.cell = cell;
    if (indexPath.row > 2) {
        UIView *selectionView = [[UIView alloc] init];
        selectionView.frame = CGRectMake(0, 0, cell.width, cell.height);
        selectionView.backgroundColor = JMColor(245, 245, 245);
        cell.selectedBackgroundView = selectionView;
        
        [UIView animateWithDuration:0.3 animations:^{
            self.upView.y = JMPhoneHeight - 207;
        }];
    } else {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [UIView animateWithDuration:0.3 animations:^{
            self.upView.y = JMPhoneHeight;
        }];
    }
}

#pragma mark - JMUpViewDelegate方法

/**
 *  获得upView中选择的文字,将该文字赋值给点击的cell,显示确定再生按钮
 */
- (void)upVIew:(JMUpView *)upVIew clickConfirmButton:(NSString *)number {
    if (!number) {
        number = @"0001";
    }
    self.cell.inputField.font = [UIFont systemFontOfSize:14];
    self.cell.inputField.text = number;
    self.cell.selected = NO;
}

#pragma mark - JMWriteCodeCellDelegate
- (void)writeCodeCell:(JMWriteCodeCell *)cell didSelectedRows:(long)rows {
    self.cell.selected = NO;
}
@end
